package GUI;

import java.util.ArrayList;

import javax.swing.JPanel;
import java.awt.SystemColor;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import Client.BusinessImplements;
import Controler.Delete_Copy_controler;
import Controler.Update_Book_Details_controler;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
/**
  * @author Lucie
 * Class Delete_Copy_secon_Panel
 * extend  Abstract_Jpanel
 * purpose: this panel get copy number of item from the user and call the controller 
 * to delete this  copy
 */
public class Delete_Copy_secon_Panel extends Abstract_Jpanel {

	/**
	 * Create the panel.
	 */
	private JLabel labelbookname ;
	private ArrayList<String> item_copys;
	private JComboBox comboBox;
	private String book_id;
	private String book_name;
	public Delete_Copy_secon_Panel() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(22, 57, 645, 318);
		add(panel);
		panel.setLayout(null);
		
		labelbookname = new JLabel(""); 
		labelbookname.setFont(new Font("MV Boli", Font.BOLD, 20));
		labelbookname.setBounds(23, 11, 591, 45);
		panel.add(labelbookname);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setBounds(70, 67, 465, 221);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblCopysOfItem = new JLabel("copys of item:");
		lblCopysOfItem.setBounds(40, 26, 99, 31);
		panel_1.add(lblCopysOfItem);
		lblCopysOfItem.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		comboBox = new JComboBox();
		comboBox.setBounds(170, 33, 134, 20);
		panel_1.add(comboBox);
		
		JLabel lblChooseTheCopy = new JLabel("choose the copy you like to delete");
		lblChooseTheCopy.setBounds(66, 71, 278, 20);
		panel_1.add(lblChooseTheCopy);
		lblChooseTheCopy.setForeground(Color.RED);
		lblChooseTheCopy.setFont(new Font("Segoe Print", Font.PLAIN, 13));
		
		JLabel lblDeleteACopy = new JLabel("Delete a Copy");
		lblDeleteACopy.setForeground(new Color(25, 25, 112));
		lblDeleteACopy.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblDeleteACopy.setBounds(22, 11, 324, 35);
		add(lblDeleteACopy);
		/*
		 *btnDelete=When you click this button you will call deleteCopy_controler.delete_Copy(comboBox.getSelectedItem().toString())
		 * a method of the controller of this panel if this method return true we will
		 * call the method get_all_copys that return array list with all the copies of the
		 * current item than we will call the methods of this class next_screen_success(),empty_text_fields()
		 * call the methods of this class next_screen_success(),on_screen_load(item_copys,book_name,book_id); to initial this panel again
		 * else we call next_screen_faild(deleteCopy_controler.get_msg());	
		 * deleteCopy_controler.get_msg(); return the msg from the controller with all
		 * the error msg.
		 */
		JButton btnDelete = new JButton("delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (!comboBox.getSelectedItem().toString().isEmpty())
		 		{
					Delete_Copy_controler deleteCopy_controler=new Delete_Copy_controler();
					
								if(!deleteCopy_controler.delete_Copy(comboBox.getSelectedItem().toString()))
								{
									 next_screen_faild(deleteCopy_controler.get_msg());	
								}
								else
								{
			
									ArrayList<String> item_copys=deleteCopy_controler.get_all_copys(book_id);
									if (!(item_copys==null))
							 		{   next_screen_success(); 
										on_screen_load(item_copys,book_name,book_id);
							 		}
								}
		 		}
			}
		});
		btnDelete.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnDelete.setBounds(536, 386, 97, 35);
		add(btnDelete);
		/*
		 * btnBack ;When you click this button you will return to 
		 *	Detete_Copy_first_Panel screen
		 */
		JButton btnBack = new JButton("back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Detete_Copy_first_Panel next_screen=new Detete_Copy_first_Panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(next_screen);
				next_screen.setSize(getMaximumSize());
				next_screen.on_screen_load();
				
			}
		});
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnBack.setBounds(47, 386, 97, 35);
		add(btnBack);

	}
	
	/**
	 * 
	 * @param item_copy
	 * @param bookname
	 * @param bookid
	 * 	this method is the first method we call in this screen
	 * the main purpose of this method is to initialize the comboBox 
	 * that this panel use, here we call the controller of this panel
	 * to get all author names and id's and set them at the combobox
	 */
	public void on_screen_load(ArrayList<String> item_copy,String bookname,String bookid) {
		book_id=new String ();
		book_name=new String ();
		
		book_id=bookid;
		book_name=bookname;
		labelbookname.setText("copys of the item "+book_name+":");
		  item_copys=item_copy;
		 add_all_copys_tocommbobox(item_copy);
		  
		 
	}
	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void on_screen_load() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * initial a String for the commbobox
	 * @param all_copys
	 */
	public void add_all_copys_tocommbobox(ArrayList<String> all_copys)
	{ 
		String[] copys=new String[all_copys.size()+1];
		
		copys[0] = "";
	
		for (int i = 1 ; i <= all_copys.size() ; i++)
			copys[i] = all_copys.get(i-1);
			
		
		comboBox.setModel(new DefaultComboBoxModel(copys));
	
	}
	/**
	 * this method display showMessageDialog with a success msg
	 */
	public void next_screen_success()
	{
		JOptionPane.showMessageDialog(this,"apply success");
	}
	/**
	 *  this method display showMessageDialog with a failed msg
	 * @param msg
	 */
	public void next_screen_faild(String msg)
	{
		JOptionPane.showMessageDialog(this,"apply faild "+msg);
	}
	
	
	
}
